<template>
	<view class="cu-bar bg-white tabbar border shop foot" style="background-color: #f1f1f1;"
		v-if="totalNum > 0">
	
	
		<button class="action" style="width: 180px;">
			<view class="grid col-1 text-right padding-right-lg">
				<view class="text-bold text-lg margin-top-xs">￥{{totalPrice}}</view>
				<view class="text-grey  margin-top-xs">餐盒费￥0，配送费￥3</view>
			</view>
		</button>
		<view class="bg-gradual-blue cu-btn round " @tap="selectFlavor">{{flavorName}}</view>
		<view class="bg-orange submit" @tap="cat()">结算</view>
	</view>
</template>

<script>
	import {mapState,mapMutations} from 'vuex'
	import request from '@/common/request.js'
	props:{
		flavor:{
			type:number,
			default:1
		}
	}
	data(){
		return {
			// flavor:1,
			flavorName:'正常',
			arr_sel:[]
		}
	},
	computed(){
		...mapState(['totalNum','totalPrice','dishWithType'])
	},
	methods:{
		...mapMutations(['clearCart'])
		// 菜单内容
		cat() {
		
			// 点击结算之后对菜品进行处理
			this.dishWithType.forEach((item, indexs) => {
				item.forEach((dish, index) => {
					if (dish.num > 0) {
						// console.log('当前是', dish.name, '数量大于0', indexs, index)
						this.arr_sel.push(dish)
					}
				})
			})
			uni.showModal({
				title: '确认支付',
				content: this.arr_sel.map(item => item.name + '-' + item.num + '-' + item.num * item.price +
						'\n')
					.join(''),
				success: res => {
					if (res.confirm) {
						console.log('点击了确认')
						const obj = {
							"total": this.totalPrice,
							"flavor_id": this.flavor,
							dishes: this.arr_sel
						}
		
						request({
							url: '/order',
							method: 'POST',
							data: obj
						}).then(res => {
							console.log(res)
							uni.showToast({
								title: '支付成功',
								duration: 2000
							})
							// 关闭弹窗
							// this.modalName = 0
							// 清空购物车
							this.clearCart()
							// 跳转路由
							// uni.switchTab({
							// 	url:'../order/order'
							// })
							
						}).catch(e => {
							console.log(e)
							uni.showToast({
								title: '支付失败',
								icon: "error",
								duration: 2000
							})
						})
		
					} else if (res.cancel) {
						uni.showToast({
							title: '支付取消',
							icon: 'error',
							duration: 2000
						}, 2000)
					}
				}
			})
			// }
		
			setTimeout(() => {
				uni.hideToast()
			})
		},
		
		selectFlavor(){
			
		}
	}
</script>

<style>
</style>
